//
//  ViewController.m
//  Rac
//
//  Created by 崔博 on 2017/12/28.
//  Copyright © 2017年 崔博. All rights reserved.
//

#import "ViewController.h"
#import <ReactiveObjC/ReactiveObjC.h>
#import <ReactiveObjC/RACReturnSignal.h>
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    // flatenMap 一般用于信号中的信号
    RACSubject * subOfsub = [RACSubject subject];
    RACSubject * sub = [RACSubject subject];
    
    
//    [subOfsub subscribeNext:^(RACSignal * x) {
//        [x subscribeNext:^(id  _Nullable x) {
//            NSLog(@"%@",x);
//        }];
//    }];
//    [[subOfsub switchToLatest] subscribeNext:^(id  _Nullable x) {
//        NSLog(@"%@",x);
//    }];
    [[subOfsub flattenMap:^__kindof RACSignal * _Nullable(id  _Nullable value) {
        return value;
    }] subscribeNext:^(id  _Nullable x) {
        NSLog(@"%@",x);
    }];
  
    
    // 发送洗好
    [subOfsub sendNext:sub];
    [sub sendNext:@"123"];
}
- (void)map{
    // 映射
    RACSubject * sub = [RACSubject subject];
    
    [[sub map:^id _Nullable(id  _Nullable value) {
        NSLog(@"%@",value);
        // 返回数据类型
        return value;
    }] subscribeNext:^(id  _Nullable x) {
        NSLog(@"%@",x);
    }];
    
    [sub sendNext:@"111"];
    
}
- (void)flattenMap{
    
    
    RACSubject * sub = [RACSubject subject];
    
    // 绑定信号
    RACSignal * blindSingal = [sub flattenMap:^__kindof RACSignal * _Nullable(NSString * value) {
        NSLog(@"%@",value);
        
        // 包装修改过的内容
        NSString * str = [NSString stringWithFormat:@"%@xxxxxx",value];
        return [RACReturnSignal return:str];
    }];
    
    
    [blindSingal subscribeNext:^(id  _Nullable x) {
        NSLog(@"%@",x);
    }];
    
    [sub sendNext:@"123"];
}

@end
